<?php

// No direct access

defined('_JEXEC') or die('Restricted access');

jimport('joomla.filesystem.file');

$document = &JFactory::getDocument();

$model = $this->model;

$dest_dir = JPATH_COMPONENT.DS.'data';
$max_file_size = 1024;

$post_table =
    "<table border='0' cellspacing='2' cellpadding='1' align='center'>
            <thead>
                <tr>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <p>Puoi caricare una immagine (gia' provvista di geotag) di dimensioni non superiori ad 1 MB.</p>
                    </td>
                </tr>
                <tr>
                    <td>
                        <form name='upload' method='post' enctype='multipart/form-data'>
                            <input type='file' name='uploadfile'>
                            <input type='text' name='imageName'>
                            <br>
                            <input type='submit' name='go' value='Carica'>
                        </form>
                    </td>
                </tr>
            </tbody>
        </table>";


$file = JRequest::getVar('uploadfile', null, 'files', 'array');
$imageName = JRequest::getVar('imageName');

if(isset($file) && isset($imageName)) {

    $filename = JFile::makeSafe($file['name']);

    if($file['size'] > $max_file_size) $msg = JText::_('the maximum file size allowed is: ').' '.$max_file_size;

    $src = $file['tmp_name'];
    $dest = $dest_dir . DS . $filename;

    if ($file['type'] == 'image/jpeg' || $file_type == '*') {
        if ( JFile::upload($src, $dest) ) {

            //$model->insertImage('', '', $file['name'], $dest, 'icon');
            $model->insertImage('', '', $imageName, $dest, 'icon');
            $msg = JText::_('Your image has been uploaded to: ').' '.$dest.'<a href=\'index.php?option=com_geophoto&controller=base&task=display\'> HOME </a>';

            print_r($model->getImageLocation($file['tmp_name']));
        } else {

            $msg = JText::_('Error during upload, please try again.');
        }
    } else {

        $msg = JText::_('File type not allowed: ').' '.$file['type'];
    }

    echo $msg;

    // print location
    $value = $model->getImageLocation($dest);
    if($value["latitude"] == -1 || $value["longitude"] == -1){
    	"The image you uploaded is not tagged";
    }else{
    	echo "latitude: ".$value["latitude"];
    	echo "<br>";
    	echo "longitude: ".$value["longitude"];
    }

}else if(isset($file) && !isset($imageName)){

    echo "<center><b>Specificare un nome per l'immagine!</b></center>";
    echo $post_table;

}else{

    echo $post_table;

}


?>



